package edu.ts.crowdfunding.category.Controller;

import edu.ts.crowdfunding.category.domain.Category;
import edu.ts.crowdfunding.category.domain.PO.CategoryConditionalQueryPO;
import edu.ts.crowdfunding.category.domain.PO.CategoryUpdatePO;
import edu.ts.crowdfunding.category.service.CategoryService;
import edu.ts.crowdfunding.category.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Tag(name = "产品分类相关接口", description = "分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/list")
    @Operation(summary = "获取产品分类列表",description = "1:成功，0:失败")
    public Result list(@RequestBody CategoryConditionalQueryPO categoryConditionalQueryPO) {
        return categoryService.pageSearch(categoryConditionalQueryPO);
    }

    @PostMapping("/update")
    @Operation(summary = "更新分类信息",description = "1:成功，0:失败")
    public Result update(@RequestBody CategoryUpdatePO categoryUpdatePO) {
        return categoryService.updateCategory(categoryUpdatePO);
    }

    @PostMapping("/status/{id}/{status}")
    @Operation(summary = "启用/禁用分类",description = "1:成功，0:失败")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status) {
        return categoryService.updateStatus(id,status);
    }

    @PostMapping("/add")
    @Operation(summary = "新增分类",description = "1:成功，0:失败")
    public Result add(@RequestBody Category category){
        Result result = new Result<>();
        try {
            if(category.getCategoryName()!=null &&!category.getCategoryName().isEmpty() &&category.getCategorySortOrder()!=0){
                category.setCategoryCreatedAt(new java.util.Date());
                category.setCategoryUpdatedAt(new java.util.Date());
                categoryService.save(category);
                result.setCode(1);
            }else {
                result.setCode(0);
            }
        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }

        return result;
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除分类和关联的产品",description = "1:成功，0:失败")
    public Result delete(@PathVariable Long id){
        return categoryService.deleteWithProducts(id);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID获取分类信息", description = "成功:1 失败:0")
    public Result get(@PathVariable Long id) {
        Result result = new Result();
        try {
            Category category = categoryService.getById(id);
            if(category != null ){
                result.setData(category);
                result.setCode(1);
            }else {
                result.setCode(0);
            }

        } catch (Exception e) {
            result.setCode(0);
            throw new RuntimeException(e);
        }
        return result;
    }

    @GetMapping("/getCategoryList")
    @Operation(summary = "获取所有分类", description = "用于下拉列表")
    public Result getCategoryList(){
        List<Category> list = categoryService.list();
        return new Result<>(1,"查询成功",list);
    }
}
